﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dcms.BLL;
using Dcms.Model;
using Dcms.Common;
using Dcms.TempLate;

public partial class cate : System.Web.UI.UserControl
{
    Dcms_CateBLL cBll = new Dcms_CateBLL();
    //从数据库取出的记录数
    public int TotalItem = 0;
    //数据模板样式
    public string[] StyleArr;
    //模板头样式
    public string[] headStyleArr;
    //模板尾样式
    public string[] footStyleArr;
    //模板数
    public int TempTotalNum;
    //要展示几条
    public int ItemTotalNum;
    //实际展示的数据条数
    public int ActualNum;
    //Dcms Repeater的一些属性
    public string IsPage = "FALSE";
    //数据库查询语句
    public string sqlStr= "Select * From Dcms_Cate Where 1=1 ";
    public string sqlNum = "Select Count(Cate_ID) From Dcms_Cate Where 1=1 ";
    protected void Page_Load(object sender, EventArgs e)
    {
        string thisSqlstr = sqlStr;
        string thisSqlnum = sqlNum;
        //页码默认值
        int Page = 1;
        //取记录数
        string TotalNum = "0";
        //取条件
        string Conditionstr = getCondition(cate_Repeater.SetCondition);
        //条件组合
        if (Conditionstr.Length > 0)
        {
            thisSqlstr = thisSqlstr + Conditionstr;
            thisSqlnum = thisSqlnum + Conditionstr;
        }
        //Sql语句合并
        thisSqlstr = thisSqlstr + " Order By " + getOrder(cate_Repeater.SetOrder) + " " + getSort(cate_Repeater.SetSort);
        //处理分页
        if (cate_Repeater.SetIsPage.ToUpper() != "FALSE")
        {
            IsPage = "True";
        }
        //处理风格字符串
        StyleArr = this.cate_Repeater.SetStyleStr.Split('|');
        headStyleArr = this.cate_Repeater.SetHeadStyle.Split('|');
        footStyleArr = this.cate_Repeater.SetFootStyle.Split('|');
        //风格种类
        TempTotalNum = StyleArr.Length;
        //默认取多少条
        ItemTotalNum = Utils.StrToInt(cate_Repeater.SetNum, 0);
        //实际取多少条
        ActualNum = (TempTotalNum > ItemTotalNum) ? TempTotalNum : ItemTotalNum;
        //是否要分页并作处理
        if (IsPage.ToUpper() == "TRUE")
        {
            Page = DcmsRequest.GetQueryInt("Page", 1);
            TotalNum = cBll.GetDataCount(thisSqlnum);
            //把总数写入到Cookie
            Utils.WriteCookie("TotalRecord", TotalNum);
            Utils.WriteCookie("PageSize", ActualNum.ToString());
        }
        //取数据并绑定
        IList<Dcms_CateModel> cModelList = cBll.GetDataListByPage(thisSqlstr, Page, ActualNum);
        TotalItem = cModelList.Count;
        cate_Repeater.DataSource = cModelList;
        cate_Repeater.DataBind();
    }
    /// <summary>
    /// 取排序
    /// </summary>
    /// <param name="orderString"></param>
    /// <returns></returns>
    protected string getOrder(string orderString)
    {
        string Order = string.Empty;
        //处理排序属性
        if ((orderString.ToUpper() != "ID") && (orderString.Length > 0))
        {
            Order = orderString.ToUpper();
        }
        //取排序的字段
        switch (Order)
        {
            case "ID":
                Order = "Cate_ID";
                break;
            case "TIME":
                Order = "Cate_AddTime";
                break;
            case "ORDER":
                Order = "Cate_Order";
                break;
            default:
                Order = "Cate_ID";
                break;
        }
        return Order;
    }
    /// <summary>
    /// 取SQL升序或者是降序
    /// </summary>
    /// <param name="sortString"></param>
    /// <returns></returns>
    protected string getSort(string sortString)
    {
        string Sort = string.Empty;
        if ((sortString.ToUpper() != "DESC") && (sortString.Length > 0))
        {
            Sort = "Asc";
        }
        return Sort;
    }
    /// <summary>
    /// 取SQL条件
    /// </summary>
    /// <param name="conditionString"></param>
    /// <returns></returns>
    protected string getCondition(string conditionString)
    {
        int CateId = 0;
        string Condition = string.Empty;
        int QueryCateId = DcmsRequest.GetQueryInt("CateId", 0);
        string KeyWord = Utils.ChkSQL(Request["KeyWord"]);
        if (conditionString.Length > 0)
        {
            CateId = Utils.StrToInt(conditionString, 0);
            if (CateId == 0)
            {
                string SplitCondition = conditionString.ToUpper();
                string[] ConditionArr = SplitCondition.Split(',');
                //是否有默认值
                string CateCondition = string.Empty;
                int Cate_i = 1;
                for (int i = 0; i < ConditionArr.Length; i++)
                {
                    if (Utils.StrToInt(ConditionArr[i], 0) > 0)
                    {
                        if (Cate_i == 1)
                        {
                            if (cBll.GetCateIsChild(Utils.StrToInt(ConditionArr[i], 0)) == 0)
                            {
                                CateCondition = cBll.GetChildCateId(Utils.StrToInt(ConditionArr[i], 0));
                            }
                            else
                            {
                                CateCondition = Utils.StrToInt(ConditionArr[i], 0).ToString();
                            }
                            Cate_i = Cate_i + 1;
                        }
                        else
                        {
                            if (cBll.GetCateIsChild(Utils.StrToInt(ConditionArr[i], 0)) == 0)
                            {
                                CateCondition = CateCondition + "," + cBll.GetChildCateId(Utils.StrToInt(ConditionArr[i], 0));
                            }
                            else
                            {
                                CateCondition = CateCondition + "," + Utils.StrToInt(ConditionArr[i], 0);
                            }
                        }
                    }
                }
                //取图片类别IsImage
                if (SplitCondition.IndexOf("ISIMAGE") >= 0)
                {
                    Condition = Condition + " And Cate_Image>=1";
                }
                //由Query取值
                if (SplitCondition.IndexOf("QUERY") >= 0)
                {
                    if (QueryCateId > 0)
                    {
                        Condition = Condition + " And Cate_Id=" + QueryCateId;
                    }
                    if (KeyWord.Length > 0)
                    {
                        KeyWord = String.Format("Cate_Name Like '%{0}%'", KeyWord);
                        Condition = Condition + " And " + KeyWord;
                    }
                }
                //由Query取值结束
                if (Condition.IndexOf("Cate_Id") >= 0)
                {
                    Condition = Condition;
                }
                else
                {
                    if (CateCondition.Length > 0)
                    {
                        Condition = Condition + " And Cate_Id In (" + CateCondition + ")";
                    }
                    else
                    {
                        Condition = Condition;
                    }
                }
            }
            else
            {
                Condition = Condition + " And Cate_ParentID=" + CateId;
            }
        }
        return Condition;
    }
    /// <summary>
    /// 渲染类别数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cate_Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Literal cateItemTemplate = (Literal)e.Item.FindControl("cateDisplay");
        Dcms_CateModel cModel = (Dcms_CateModel)e.Item.DataItem;

        if (TempTotalNum > 1)
        {//当模板风格大于1时的渲染方式
            if (cModel.Cate_IsChild == 0)
            {
                string cateItemString = string.Empty;
                cateItemString = TempRender.TempRenderCate(cModel, StyleArr[0], e.Item.ItemIndex);
                cateItemString = cateItemString + "\n" + GetChildTypeString(cModel.Cate_ID, cModel,1);
                cateItemTemplate.Text = cateItemString;
            }
            else
            {
                cateItemTemplate.Text = TempRender.TempRenderCate(cModel, StyleArr[0], e.Item.ItemIndex);
            }
        }
        else
        {//当模板风格只有1种时的渲染方式
            cateItemTemplate.Text = TempRender.TempRenderCate(cModel, StyleArr[0], e.Item.ItemIndex);
        }
    }
    /// <summary>
    /// 取下级类别
    /// </summary>
    /// <param name="CateId"></param>
    /// <returns></returns>
    protected string GetChildTypeString(int CateId, Dcms_CateModel cModel, int LayerNum)
    {
        string cateString = string.Empty;
        int Layer = LayerNum;
        //头Head样式
        cateString = TempRender.TempRenderCate(cModel, headStyleArr[Layer], 0);
        string childSql = sqlStr + getCondition(CateId.ToString()) + " Order By " + getOrder(cate_Repeater.SetOrder) + " " + getSort(cate_Repeater.SetSort);
        IList<Dcms_CateModel> cModelList = cBll.GetDataListByPage(childSql, 1, 20);
        for (int i = 0; i < cModelList.Count; i++)
        {
            if (cModelList[i].Cate_IsChild == 0)
            {
                cateString = cateString + TempRender.TempRenderCate(cModelList[i], StyleArr[1], i);
                cateString = cateString + footStyleArr[Layer + 1];
                cateString = cateString + GetChildTypeString(cModelList[i].Cate_ID, cModelList[i], 2);
            }
            else
            {
                cateString = cateString + TempRender.TempRenderCate(cModelList[i], StyleArr[Layer], i);
                cateString = cateString + footStyleArr[Layer + 1];
            }
        }
        return cateString;
    }
}
